var PeopleMedia = PeopleMedia || {};
PeopleMedia.Connect = new function () {
    this.Actions = {};
    this.PerPage = 10;
    this.CurrentTab;
    this.FavoritesTab = 'Favorites';
    this.YesesTab = 'Yeses';
    this.ViewsTab = 'Views';
    this.YesesTabEnum = 0;
    this.FavoritesTabEnum = 1;
    this.ViewsTabEnum = 2;
    this.FavoriteCount = 0;
    this.ViewCount = 0;
    this.YesCount = 0;
    this.SentFilter = 'Sent';
    this.ReceivedFilter = 'Received';
    this.MutualFilter = 'Mutual';
    this.MaximumMessageLength = 3000;
    this.suppressFlash = false;
    this.MutualYesCount = 0;
    this.IncomingYesCount = 0;
    this.IncomingFavoriteCount = 0;
    this.MutualFavoriteCount = 0;
    this.SuppressMutualFavoritePopUp = false;
    this.SuppressMutualYesPopUp = false;
    this.FeatureNames = { Views: "connectviews", Favorites: "connectfavs", Yes: "connectyes" };
    this.PlaceHolderText = 'Write a quick message...';
    this.AddFavoriteUrl = FRONT_SITE_URL+'ajax/Primary/SaveToFavorites';
    this.RemoveFavoriteUrl = FRONT_SITE_URL+'ajax/Primary/Unfavorite';
    this.SendBirthdayFlirtUrl = FRONT_SITE_URL+'ajax/Primary/SendBirthdayFlirt';
    this.GetFavoriteUrl = FRONT_SITE_URL+'home/Connect/GetFavorites';
    this.GetViewUrl = FRONT_SITE_URL+'home/Connect/GetViews';
    this.GetYesVotesUrl = FRONT_SITE_URL+'home/Connect/GetYesVotes';
    this.SendMessageUrl = FRONT_SITE_URL+'home/Connect/SendMessage';   //xxx
    this.ReportConcernUrl = FRONT_SITE_URL+'ajax/Primary/ReportConcern';
    this.LikePhotoUrl = FRONT_SITE_URL+'ajax/Primary/LikePhoto';
    this.GetOneClickFlirtUrl = FRONT_SITE_URL+'ajax/Primary/SendOneClickFlirt';
    this.GetMutualYesVoteUrl = FRONT_SITE_URL+'home/Connect/GetMutualYesVotes';
    this.GetSentYesVoteUrl = FRONT_SITE_URL+'home/Connect/GetSentYesVotes';
    this.GetReceivedYesVoteUrl = FRONT_SITE_URL+'home/Connect/GetReceivedYesVotes';
    this.GetMutualFavoriteUrl = FRONT_SITE_URL+'home/Connect/GetMutualFavorites';
    this.GetSentFavoriteUrl = FRONT_SITE_URL+'home/Connect/GetSentFavorites';
    this.GetReceivedFavoriteUrl = FRONT_SITE_URL+'home/Connect/GetReceivedFavorites';
    this.GetSentViewsUrl = FRONT_SITE_URL+'home/Connect/GetSentViews';
    this.GetReceivedViewsUrl = FRONT_SITE_URL+'home/Connect/GetReceivedViews';
    this.RefreshPaginationURL = FRONT_SITE_URL+'home/Connect/RefreshPagination';
    this.RefreshPageCountsUrl = FRONT_SITE_URL+'home/Connect/RefreshPageCounts';
    this.DeleteConnectItemUrl = FRONT_SITE_URL+'home/Connect/DeleteConnectItem';
    this.DeleteMultipleConnectItemsUrl = FRONT_SITE_URL+'home/Connect/DeleteMultipleConnectItems';

    this.loadFavorites = function () {
        PeopleMedia.Connect.showTabFavorites();
        PeopleMedia.Connect.refreshTabCounts(PeopleMedia.Connect.FavoritesTabEnum);
        if (!PeopleMedia.Connect.FavoriteLoaded) {
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetFavoriteUrl,
                dataType: 'json',
                success: function (response) {
                    if (response.status == "Success") {
                        $jq('#FavoriteContent').empty().html(response.html);
                        PeopleMedia.Connect.initFavorites(PeopleMedia.Connect.SentFilter);
                        PeopleMedia.Connect.FavoriteLoaded = true;

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
        }


    };
    this.loadViews = function () {
        PeopleMedia.Connect.showTabViews();
        PeopleMedia.Connect.refreshTabCounts(PeopleMedia.Connect.ViewsTabEnum);
        if (!PeopleMedia.Connect.ViewLoaded) {
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetViewUrl,
                dataType: 'json',
                success: function (response) {
                    if (response.status == "Success") {
                        $jq('#ViewContent').empty().html(response.html);
                        PeopleMedia.Connect.initViews(PeopleMedia.Connect.SentFilter);
                        PeopleMedia.Connect.ViewLoaded = true;

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
        }
    };
    this.loadYesVotes = function () {
        PeopleMedia.Connect.showTabYesVotes();
        PeopleMedia.Connect.refreshTabCounts(PeopleMedia.Connect.YesesTabEnum);
        if (!PeopleMedia.Connect.YesVotesLoaded) {
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetYesVotesUrl,
                dataType: 'json',
                success: function (response) {
                    if (response.status == "Success") {
                        $jq('#YesVoteContent').empty().html(response.html);
                        PeopleMedia.Connect.initYesVotes(PeopleMedia.Connect.SentFilter);
                        PeopleMedia.Connect.YesVotesLoaded = true;

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
        }
    };

    this.loadYesReceived = function (page) {
        PeopleMedia.Connect.showTabYesReceived();
        if (!PeopleMedia.Connect.ReceivedYes) {
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetReceivedYesVoteUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#YesesListContent').html(response.html);
                            PeopleMedia.Connect.setYesReceived(page, response.totalRecords);
                        }
                        else {
                            $jq('#YesesListContent').fadeOut("fast", function () {
                                $jq('#YesesListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setYesReceived(page, response.totalRecords);
                            });
                        }

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
        }
    };
    this.setYesReceived = function (page, totalRecords) {
        $jq('#yesCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentYes = false;
        PeopleMedia.Connect.ReceivedYes = true;
        PeopleMedia.Connect.MutualYes = false;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.YesesTabEnum);
        //PeopleMedia.ReloadAds();
        PeopleMedia.Connect.hideYesVoteCountPopUp();
    };
    this.loadYesSent = function (page) {
        PeopleMedia.Connect.showTabYesSent();
        if (!PeopleMedia.Connect.SentYes) {
           
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetSentYesVoteUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#YesesListContent').html(response.html);
                            PeopleMedia.Connect.setYesSent(page, response.totalRecords);
                        }
                        else {
                            $jq('#YesesListContent').fadeOut("fast", function () {
                                $jq('#YesesListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setYesSent(page, response.totalRecords);

                            });
                        }

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
        }
    };
    this.setYesSent = function (page, totalRecords) {
        $jq('#yesCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();

        PeopleMedia.Connect.SentYes = true;
        PeopleMedia.Connect.ReceivedYes = false;
        PeopleMedia.Connect.MutualYes = false;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.YesesTabEnum);
        //PeopleMedia.ReloadAds();
    };
    this.loadYesMutual = function (page) {
        PeopleMedia.Connect.showTabYesMutual();

        if (!PeopleMedia.Connect.MutualYes) {
           
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetMutualYesVoteUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#YesesListContent').html(response.html);
                            PeopleMedia.Connect.setYesMutual(page, response.totalRecords);
                        }
                        else {
                            $jq('#YesesListContent').fadeOut("fast", function () {
                                $jq('#YesesListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setYesMutual(page, response.totalRecords);

                            });
                        }

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
          
        }
    };
    this.setYesMutual = function (page, totalRecords) {
        $jq('#yesCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();

        PeopleMedia.Connect.SentYes = false;
        PeopleMedia.Connect.ReceivedYes = false;
        PeopleMedia.Connect.MutualYes = true;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.YesesTabEnum);
        //PeopleMedia.ReloadAds();
        PeopleMedia.Connect.hideYesMutualPopUp();
        PeopleMedia.Connect.SuppressMutualYesPopUp = true;
    };

    this.loadFavoriteReceived = function (page) {

        PeopleMedia.Connect.showTabFavoriteReceived();
        if (!PeopleMedia.Connect.ReceivedFavorite) {
           
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetReceivedFavoriteUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#FavoritesListContent').html(response.html);
                            PeopleMedia.Connect.setFavoriteReceived(page, response.totalRecords);
                        }
                        else {
                            $jq('#FavoritesListContent').fadeOut("fast", function () {
                                $jq('#FavoritesListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setFavoriteReceived(page, response.totalRecords);
                            });
                        }

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
            
        }
    };
    this.setFavoriteReceived = function (page, totalRecords) {
        $jq('#favoriteCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentFavorite = false;
        PeopleMedia.Connect.ReceivedFavorite = true;
        PeopleMedia.Connect.MutualFavorite = false;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.FavoritesTabEnum);
        //PeopleMedia.ReloadAds();
        PeopleMedia.Connect.hideFavoriteCountPopUp();
    };
    this.loadFavoriteSent = function (page) {
        PeopleMedia.Connect.showTabFavoriteSent();

        if (!PeopleMedia.Connect.SentFavorite) {
          
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetSentFavoriteUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#FavoritesListContent').html(response.html);
                            PeopleMedia.Connect.setFavoriteSent(page, response.totalRecords);
                        }
                        else {
                            $jq('#FavoritesListContent').fadeOut("fast", function () {
                                $jq('#FavoritesListContent').empty().html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setFavoriteSent(page, response.totalRecords);
                            });
                        }
                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });

        }
    };
    this.setFavoriteSent = function (page, totalRecords) {
        $jq('#favoriteCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentFavorite = true;
        PeopleMedia.Connect.ReceivedFavorite = false;
        PeopleMedia.Connect.MutualFavorite = false;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.FavoritesTabEnum);
        //PeopleMedia.ReloadAds();

    };
    this.loadFavoriteMutual = function (page) {
        PeopleMedia.Connect.showTabFavoriteMutual();
        if (!PeopleMedia.Connect.MutualFavorite) {
          
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetMutualFavoriteUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#FavoritesListContent').html(response.html);
                            PeopleMedia.Connect.setFavoriteMutual(page, response.totalRecords);
                        }
                        else {
                            $jq('#FavoritesListContent').fadeOut("fast", function () {
                                $jq('#FavoritesListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setFavoriteMutual(page, response.totalRecords);

                            });
                        }


                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
            
        }
    };
    this.setFavoriteMutual = function (page, totalRecords) {
        $jq('#favoriteCount').empty().html(totalRecords + ' Results');

        PeopleMedia.Connect.initTextArea();

        PeopleMedia.Connect.SentFavorite = false;
        PeopleMedia.Connect.ReceivedFavorite = false;
        PeopleMedia.Connect.MutualFavorite = true;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.FavoritesTabEnum);
        //PeopleMedia.ReloadAds();
        PeopleMedia.Connect.hideFavoriteMutualPopUp();
        PeopleMedia.Connect.SuppressMutualFavoritePopUp = true;
    };
    this.loadViewsReceived = function (page) {
        PeopleMedia.Connect.showTabViewsReceived();

        if (!PeopleMedia.Connect.ReceivedViews) {
            
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetReceivedViewsUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#ViewsListContent').html(response.html);
                            PeopleMedia.Connect.setViewsReceived(page, response.totalRecords);
                        }
                        else {
                            $jq('#ViewsListContent').fadeOut("fast", function () {
                                $jq('#ViewsListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setViewsReceived(page, response.totalRecords);
                            });
                        }

                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
            
        }
    };
    this.setViewsReceived = function (page, totalRecords) {
        $jq('#viewCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentViews = false;
        PeopleMedia.Connect.ReceivedViews = true;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.ViewsTabEnum);
        //PeopleMedia.ReloadAds();
        $jq('#viewedme').hide();
    };
    this.loadViewsSent = function (page) {
        PeopleMedia.Connect.showTabViewsSent();

        if (!PeopleMedia.Connect.SentViews) {
           
            $jq.ajax({
                type: 'GET',
                url: PeopleMedia.Connect.Actions.GetSentViewsUrl,
                data: { currentPage: page },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 'Success') {
                        if (PeopleMedia.Connect.suppressFlash) {
                            $jq('#ViewsListContent').html(response.html);
                            PeopleMedia.Connect.setViewsSent(page, response.totalRecords);
                        }
                        else {
                            $jq('#ViewsListContent').fadeOut("fast", function () {
                                $jq('#ViewsListContent').html(response.html).fadeIn("fast");
                                PeopleMedia.Connect.setViewsSent(page, response.totalRecords);
                            });
                        }


                    } else if (response.redirect) {
                        location.href = response.redirect;
                    }
                }
            });
           
        }
    };
    this.setViewsSent = function (page, totalRecords) {
        $jq('#viewCount').empty().html(totalRecords + ' Results');
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentViews = true;
        PeopleMedia.Connect.ReceivedViews = false;
        PeopleMedia.Connect.refreshPagination(page, totalRecords);
        preventPaging = false;
        PeopleMedia.Connect.suppressFlash = false;
        PeopleMedia.Connect.refreshPage(PeopleMedia.Connect.ViewsTabEnum);
        //PeopleMedia.ReloadAds();
    };

    this.refreshPage = function (tab) {

        PeopleMedia.Connect.refreshTab(tab);
    };

    this.refreshFilterCounts = function (tab, incomingCount, mutualCount) {
        var totalCount;
        if (tab == PeopleMedia.Connect.FavoritesTabEnum) {
            $jq('#listFavoritesReceivedCount').empty().html(incomingCount);
            $jq('#listFavoritesMututalCount').empty().html(mutualCount);
            totalCount = incomingCount + mutualCount;
            $jq('#mainFavoriteCount').empty().html(totalCount);
            if (incomingCount > 0) {
                $jq('#starburstFavoritesReceivedCount').show();
            } else {
                $jq('#starburstFavoritesReceivedCount').hide();
            }
            if (mutualCount > 0) {
                $jq('#starburstFavoritesMutualCount').show();
            } else {
                $jq('#starburstFavoritesMutualCount').hide();
            }
            if (totalCount > 0) {
                $jq('#mainFavoriteCountStarBurst').show();
            } else {
                $jq('#mainFavoriteCountStarBurst').hide();
            }
        } else if (tab == PeopleMedia.Connect.YesesTabEnum) {
            $jq('#listYesesReceivedCount').empty().html(incomingCount);
            $jq('#listYesesMutualCount').empty().html(mutualCount);
            totalCount = incomingCount + mutualCount;
            $jq('#mainYesCount').empty().html(totalCount);
            if (incomingCount > 0) {
                $jq('#starburstYesesReceivedCount').show();
            } else {
                $jq('#starburstYesesReceivedCount').hide();
            }
            if (mutualCount > 0) {
                $jq('#starburstYesesMutualCount').show();
            } else {
                $jq('#starburstYesesMutualCount').hide();
            }
            if (totalCount > 0) {
                $jq('#mainYesCountStarBurst').show();
            } else {
                $jq('#mainYesCountStarBurst').hide();
            }
        } else if (tab == PeopleMedia.Connect.ViewsTabEnum) {
            $jq('#listViewsReceivedCount').empty().html(incomingCount);
            $jq('#mainViewCount').empty().html(incomingCount);
            if (incomingCount > 0) {
                $jq('#starburstViewsReceivedCount').show();
                $jq('#mainViewCountStarBurst').show();
            } else {
                $jq('#starburstViewsReceivedCount').hide();
                $jq('#mainViewCountStarBurst').hide();
            }
        }
    };

    this.refreshTabCounts = function (tab) {

        if (PeopleMedia.Connect.FavoriteLoaded && tab != PeopleMedia.Connect.FavoritesTabEnum) {
            PeopleMedia.Connect.refreshTab(PeopleMedia.Connect.FavoritesTabEnum);
        }
        if (PeopleMedia.Connect.YesVotesLoaded && tab != PeopleMedia.Connect.YesesTabEnum) {
            PeopleMedia.Connect.refreshTab(PeopleMedia.Connect.YesesTabEnum);
        }
        if (PeopleMedia.Connect.loadViewsReceived && tab != PeopleMedia.Connect.ViewsTabEnum) {
            PeopleMedia.Connect.refreshTab(PeopleMedia.Connect.ViewsTabEnum);
        }
    };

    this.refreshTab = function (tab) {
        PeopleMedia.HeaderCounts.refreshSubHeaderCounts();
        $jq.ajax({
            type: 'GET',
            url: PeopleMedia.Connect.Actions.RefreshPageCountsUrl,
            data: { tab: tab },
            dataType: 'json',
            success: function (response) {
                if (response.status == 'Success') {
                    PeopleMedia.Connect.refreshFilterCounts(tab, response.incomingnew, response.mutualnew);
                    PeopleMedia.HeaderCounts.refreshConnectCount(response.connectTotal, response.yesvotes, response.favorites, response.views);

                } else if (response.redirect) {
                    location.href = response.redirect;
                }
            }
        });
    };

    this.showError = function (message) {
        PeopleMedia.Popups.ShowGenericMessage(message);
    };
    this.handleError = function (response) {
        if (response.redirect) {
            location.href = response.redirect;
        } else if (response.message) {
            PeopleMedia.Popups.ShowGenericMessage(response.message);
        }
    };
    this.birthdayFlirtSent = false;
    this.sendBirthdayFlirt = function (encryptedMemberId, index, tab, featureName) {
        var url = PeopleMedia.Connect.Actions.SendBirthdayFlirtUrl;
        $jq.ajax({
            type: 'POST',
            url: url,
            data: { EncodedMemberId: encryptedMemberId, FeatureName: featureName },
            dataType: 'json',
            success: function (response) {
                if (response.status == "Success") {
                    $jq('#birthDayFlirtText' + index + tab).empty().html('Birthday wish sent.');
                    $jq('div[birthdayId=' + index + tab + ']').data('onclick', function () { });
                } else {
                    PeopleMedia.Connect.handleError(response);
                }
            }
        });

    };
    var lastToggle = null;
    var lastIndex = null;
    this.toggleFavorite = function (addToFavs, encryptedMemberId, index, tab, featureName, actionType) {
        if (lastIndex == index && lastToggle && lastToggle.getTime() > ((new Date()).getTime() - 1000))
            return;
        lastToggle = new Date();
        lastIndex = index;
        var url = null;
        //PeopleMedia.ReloadAds();
        if (addToFavs) {
            $jq('.SaveToFavorites' + index + tab).hide();
            $jq('.RemoveFromFavorites' + index + tab).show();
            url = PeopleMedia.Connect.Actions.AddFavoriteUrl;
        } else {
            $jq('.RemoveFromFavorites' + index + tab).hide();
            $jq('.SaveToFavorites' + index + tab).show();
            url = PeopleMedia.Connect.Actions.RemoveFavoriteUrl;
        }
        $jq.ajax({
            type: 'POST',
            url: url,
            data: { EncodedMemberId: encryptedMemberId, FeatureName: featureName, ActionType: actionType },
            dataType: 'json',
            success: function (response) {
                if (response.status == "Success") {
                } else {
                    PeopleMedia.Connect.handleError(response);
                }
            }
        });
        return false;
    };
    var lastFlirt = null;
    var lastFlirtIndex = null;
    this.sendFlirt = function (encryptedMemberId, index, tab, featureName, actionType) {
        if (lastFlirtIndex == index && lastFlirt && lastFlirt.getTime() > ((new Date()).getTime() - 1000))
            return;
        $jq("#flirthover" + index.toString() + tab).fadeIn("fast").delay(3000).fadeOut("slow", function () { $jq(this).hide(); });
        lastFlirt = new Date();
        lastFlirtIndex = index;
        var url = PeopleMedia.Connect.Actions.GetOneClickFlirtUrl;
        //PeopleMedia.ReloadAds();
        $jq.ajax({
            type: 'POST',
            url: url,
            data: { EncodedMemberId: encryptedMemberId, FeatureName: featureName, ActionType: actionType },
            dataType: 'json',
            success: function (response) {
                if (response.status == "Success") {

                } else {
                    PeopleMedia.Connect.handleError(response);
                }
            }
        });
        return false;
    };

    var lastMessageSent = null;

    this.sendMessage = function (encryptedMemberId, index, tab, userName, tabEnum, filterEnum, page, featureName, upn, actionType) {
        if (lastMessageSent && lastMessageSent.getTime() > ((new Date()).getTime() - 3000))
            return;
        lastMessageSent = new Date();
        var message = $jq('#connectInlineMessage' + index + tab).val();
        if (message && message != 'Write a quick message...' && message != '' && message != "Write a quick thank you for choosing you in Today's Matches..." && message != "Write a quick thank you for viewing your profile..." && message != "Write a quick thank you for saving your profile as a favorite...") {
            $jq('#LoadingResultsDiv').show();
            $jq.ajax({
                type: 'POST',
                url: PeopleMedia.Connect.Actions.SendMessageUrl,
                data: {
                    EncodedMemberId: encryptedMemberId,
                    Message: message,
                    UPN: upn,
                    fromPage: '&from=connect' + tab + '&backparam=' + filterEnum + '&start=' + page,
                    FeatureName: featureName,
                    ActionType: actionType,
                    SkipCSSVerif: 'HTMLEditor'
                },
                dataType: 'json',
                success: function (response) {
                    if (response.status == "Success") {
                        //location.href = response.redirect;
                        PeopleMedia.Popups.ShowGenericMessage("Message sented");
                    } else {
                        PeopleMedia.Connect.handleError(response);
                    }
                }
                 , complete: function (response) {
                     $jq('#LoadingResultsDiv').hide();
                 }
            });
        }
        else {
            PeopleMedia.Popups.ShowGenericMessage("Write a message now to " + userName + " then click send.");
        }
    };

    this.removeItem = function (index, tab) {
        $jq('#popupshowdelete' + index + tab).show();
    };

    this.hideRemoveItemPopup = function (index, tab) {
        $jq('#popupshowdelete' + index + tab).hide();
        PeopleMedia.Connect.suppressFlash = false;
        //PeopleMedia.ReloadAds();
    };

    this.deleteConnectItem = function (encryptedMemberId, index, tab, tabEnum, filter, page, featureName) {
        $jq.ajax({
            type: 'POST',
            url: PeopleMedia.Connect.Actions.DeleteConnectItemUrl,
            data: {
                encryptedMemberId: encryptedMemberId,
                tab: tabEnum,
                filter: filter,
                featureName: featureName
            },
            dataType: 'json',
            success: function (response) {
                if (response.status == "Success") {

                    $jq('#connectItem' + index + tab).toggle("fast", function () {
                        PeopleMedia.Connect.suppressFlash = true;
                        if ($jq('#connect' + tab + 'MemberList').children(':visible').length == 0) {
                            if (page == 1) {
                                PeopleMedia.Connect.goToPage(page);
                            }
                            else {
                                PeopleMedia.Connect.goToPage(page - 1);
                            }
                        }
                        else {
                            PeopleMedia.Connect.goToPage(page);
                        }

                    });

                } else {
                    PeopleMedia.Connect.hideRemoveItemPopup(index, tab);
                }

            }
        });
    };
    this.clickSelectArea = function (tab) {
        
        if ($jq('#selectAllCheckBox' + tab).is(':checked')) {
            $jq('#selectAllCheckBox' + tab).attr('checked', false);
        }
        else {
            $jq('#selectAllCheckBox' + tab).attr('checked', true);
        }
        $jq('#selectAllCheckBox' + tab).trigger('onclick');
    };

    this.selectAllDeleteCheckBoxes = function (tab) {
    
        if ($jq('#selectAllCheckBox' + tab).is(':checked')) {
            $jq('.deleteCheckbox' + tab).attr('checked', true);
        }
        else {
            $jq('.deleteCheckbox' + tab).attr('checked', false);
        }
    };

    this.selectSingleDeleteCheckBox = function (tab, index) {
        if ($jq('#selectAllCheckBox' + tab).is(':checked')) {
            if ($jq('#checkboxDelete' + index + tab).not(':checked'))
                $jq('#selectAllCheckBox' + tab).attr('checked', false);
        }
    };

    this.deleteSelectedMembers = function (tabName, tab, filter, page, featureName) {
        var members = [];
        
        $jq.each($jq('#connect' + tabName + 'MemberList input'), function (index, value) {
            if ($jq(value).is(':checked')) {
                members.push($jq(value).attr('name'));
                $jq(value).parent('div').parent('div').toggle("fast");
            }

        });
        if (members.length < 1) {
            return;
        }

        $jq.ajax({
            type: 'POST',
            url: PeopleMedia.Connect.Actions.DeleteMultipleConnectItemsUrl,
            dataType: 'json',
            traditional: true,
            data: {
                encryptedMemberIds: JSON.stringify(members),
                tab: tab,
                filter: filter,
                featureName: featureName
            },
            success: function (response) {
              //alert("success");
                if (response.status == "Success") {

                    PeopleMedia.Connect.suppressFlash = true;
                    if ($jq('#connect' + tabName + 'MemberList').children(':visible').length == 0) {
                        if (page == 1) {
                            PeopleMedia.Connect.goToPage(page);
                        }
                        else {
                            PeopleMedia.Connect.goToPage(page - 1);
                        }
                    }
                    else {
                        PeopleMedia.Connect.goToPage(page);
                    }

                }
            },
            error: function(response){
              //alert("error");
            }
        });
    };

    preventPaging = false;
    this.goToPage = function (page) {

        if (PeopleMedia.Connect.CurrentTab == PeopleMedia.Connect.FavoritesTab) {
            if (PeopleMedia.Connect.SentFavorite) {
                PeopleMedia.Connect.SentFavorite = false;
                PeopleMedia.Connect.loadFavoriteSent(page, PeopleMedia.Connect.PerPage);
            } else if (PeopleMedia.Connect.ReceivedFavorite) {
                PeopleMedia.Connect.ReceivedFavorite = false;
                PeopleMedia.Connect.loadFavoriteReceived(page, PeopleMedia.Connect.PerPage);
            } else if (PeopleMedia.Connect.MutualFavorite) {
                PeopleMedia.Connect.MutualFavorite = false;
                PeopleMedia.Connect.loadFavoriteMutual(page, PeopleMedia.Connect.PerPage);
            }
        } else if (PeopleMedia.Connect.CurrentTab == PeopleMedia.Connect.YesesTab) {
            if (PeopleMedia.Connect.SentYes) {
                PeopleMedia.Connect.SentYes = false;
                PeopleMedia.Connect.loadYesSent(page, PeopleMedia.Connect.PerPage);
            } else if (PeopleMedia.Connect.ReceivedYes) {
                PeopleMedia.Connect.ReceivedYes = false;
                PeopleMedia.Connect.loadYesReceived(page, PeopleMedia.Connect.PerPage);
            } else if (PeopleMedia.Connect.MutualYes) {
                PeopleMedia.Connect.MutualYes = false;
                PeopleMedia.Connect.loadYesMutual(page, PeopleMedia.Connect.PerPage);
            }
        } else if (PeopleMedia.Connect.CurrentTab == PeopleMedia.Connect.ViewsTab) {
            if (PeopleMedia.Connect.SentViews) {
                //alert("ABC");
                PeopleMedia.Connect.SentViews = false;
                PeopleMedia.Connect.loadViewsSent(page, PeopleMedia.Connect.PerPage);
            } else if (PeopleMedia.Connect.ReceivedViews) {
                //alert("ABC1");
                PeopleMedia.Connect.ReceivedViews = false;
                PeopleMedia.Connect.loadViewsReceived(page, PeopleMedia.Connect.PerPage);
            }
        }
    };

    this.paginate = function (page, perPage, totalRecords) {

        if (!preventPaging) {
            preventPaging = true;

            PeopleMedia.Connect.goToPage(page);
        }
    };

    this.refreshPagination = function (page, totalRecords) {

        $jq.ajax({
            type: 'GET',
            url: PeopleMedia.Connect.Actions.RefreshPaginationURL,
            data: { page: page,
                totalRecords: totalRecords
            },
            dataType: 'json',
            success: function (response) {

                if (response.status == 'Success') {
                    if (PeopleMedia.Connect.CurrentTab == PeopleMedia.Connect.FavoritesTab) {
                        $jq('#paginationFavoritesTop').empty().html(response.html);
                        $jq('#paginationFavoritesBottom').empty().html(response.htmlBottom);
                    } else if (PeopleMedia.Connect.CurrentTab == PeopleMedia.Connect.YesesTab) {
                        $jq('#paginationYesesTop').empty().html(response.html);
                        $jq('#paginationYesesBottom').empty().html(response.htmlBottom);
                    } else if (PeopleMedia.Connect.CurrentTab == PeopleMedia.Connect.ViewsTab) {
                        $jq('#paginationViewsTop').empty().html(response.html);
                        $jq('#paginationViewsBottom').empty().html(response.htmlBottom);
                    }

                } else if (response.redirect) {
                    location.href = response.redirect;
                }
            }
        });
    };


    this.goToMutualFavorites = function () {
        if (!PeopleMedia.Connect.MutualFavorite) {

            PeopleMedia.Connect.loadFavoriteMutual(1);

        }
    };
    this.goToFavoritesSent = function () {
        if (!PeopleMedia.Connect.SentFavorite) {

            PeopleMedia.Connect.loadFavoriteSent(1);

        }
    };
    this.goToFavoritesReceived = function () {
        if (!PeopleMedia.Connect.ReceivedFavorite) {

            PeopleMedia.Connect.loadFavoriteReceived(1);

        }
    };

    this.goToMutualYesVotes = function () {
        if (!PeopleMedia.Connect.MutualYes) {

            PeopleMedia.Connect.loadYesMutual(1);

        }
    };
    this.goToYesVotesSent = function () {
        if (!PeopleMedia.Connect.SentYes) {

            PeopleMedia.Connect.loadYesSent(1);

        }
    };
    this.goToYesVotesReceived = function () {
        if (!PeopleMedia.Connect.ReceivedYes) {

            PeopleMedia.Connect.loadYesReceived(1);

        }
    };
    this.goToViewsSent = function () {
        if (!PeopleMedia.Connect.SentViews) {
          
            PeopleMedia.Connect.loadViewsSent(1);

        }
    };
    this.goToViewsReceived = function () {
        if (!PeopleMedia.Connect.ReceivedViews) {
          
            PeopleMedia.Connect.loadViewsReceived(1);

        }
    };


    this.IsMovementOrSpecialKey = function (key) {
        try {
            // caps lock, shift, ctrl, alt, end, home, left, right, up, down, pgup, pgdn, ins, del, (shift/alt/ctrl are > 1000 in CKEditor key event)
            return (key == 20 || (key >= 16 && key <= 18) || (key >= 33 && key <= 40) || key == 45 || key == 46 || key > 1000);
        } catch (ex) {
            PeopleMedia.Logger.LogExceptionWithDetail(ex, { method: "PeopleMedia.Connect.IsMovementOrSpecialKey" });
            return false;
        }
    };
    this.IsDeleteKey = function (key) {
        try {
            // backspace or delete
            return (key === 8 || key === 46);
        } catch (ex) {
            PeopleMedia.Logger.LogExceptionWithDetail(ex, { method: "PeopleMedia.Connect.IsDeleteKey" });
            return false;
        }
    };
    this.PlainTextKeyPress = function (evt, item) {
        var result = true;
        try {
            var keyCode = evt.charCode ? evt.charCode : evt.keyCode;
            if (!PeopleMedia.Connect.IsMovementOrSpecialKey(keyCode) && !PeopleMedia.Connect.IsDeleteKey(keyCode) && $jq(item).val().length >= PeopleMedia.Connect.MaximumMessageLength) {
                PeopleMedia.Connect.PlainTextTruncate(item);
                if (evt.preventDefault) {
                    evt.preventDefault();
                }
                result = false;
            }
        } catch (ex) {
            PeopleMedia.Logger.LogExceptionWithDetail(ex, { method: "PeopleMedia.Connect.PlainTextKeyPress" });
        }
        return result;
    };
    this.PlainTextPaste = function (item) {
        try {
            // Run PlainTextTruncate after paste completes
            setTimeout(function () {
                PeopleMedia.Connect.PlainTextTruncate(item);
            }, 0);
        } catch (ex) {
            PeopleMedia.Logger.LogExceptionWithDetail(ex, { method: "PeopleMedia.Connect.PlainTextPaste" });
        }
    };
    this.PlainTextTruncate = function (item) {
        try {
            if ($jq(item).val().length >= PeopleMedia.Connect.MaximumMessageLength) {
                var trimmedMessage = $jq(item).val().substring(0, PeopleMedia.Connect.MaximumMessageLength);
                $jq(item).val(trimmedMessage);
                PeopleMedia.Popups.LongMessage();
            }
        } catch (ex) {
            PeopleMedia.Logger.LogExceptionWithDetail(ex, { method: "PeopleMedia.Connect.PlainTextTruncate" });
        }
    };
    this.hideYesMutualPopUp = function () {
        if ($jq("#yesmutualPopUp").is(":visible")) {
            $jq("#yesmutualPopUp").hide();
        }

    };

    this.hideFavoriteMutualPopUp = function () {
        if ($jq("#favoritemutualPopUp").is(":visible")) {
            $jq("#favoritemutualPopUp").hide();
        }

    };

    this.hideViewPopUp = function () {
        $jq('#viewedme').hide();
    };

    this.hideYesVoteCountPopUp = function () {
        $jq("#yesincomingPopUp").hide();
        if (PeopleMedia.Connect.MutualYesCount > 0 && !PeopleMedia.Connect.MutualYes && !PeopleMedia.Connect.SuppressMutualYesPopUp) {
            $jq("#yesmutualPopUp").show();
            PeopleMedia.Connect.SuppressMutualYesPopUp = true;
        }
    };

    this.hideFavoriteCountPopUp = function () {
        $jq("#favoriteincomingPopUp").hide();
        if (PeopleMedia.Connect.MutualFavoriteCount > 0 && !PeopleMedia.Connect.MutualFavorite && !PeopleMedia.Connect.SuppressMutualFavoritePopUp) {
            $jq("#favoritemutualPopUp").show();
            PeopleMedia.Connect.SuppressMutualFavoritePopUp = true;
        }
    };

    this.showTabFavorites = function () {
        $jq("#menufavorites").removeClass("menulistareaoff color-main").addClass("menulistareaon");
        $jq("#menuyesvote").removeClass("menulistareaon").addClass("menulistareaoff color-main");
        $jq("#menuviews").removeClass("menulistareaon").addClass("menulistareaoff color-main");
        $jq("#YesVoteContent").hide();
        $jq("#ViewContent").hide();
        $jq("#FavoriteContent").show();
        PeopleMedia.Connect.CurrentTab = PeopleMedia.Connect.FavoritesTab;
        //PeopleMedia.ReloadAds();
    };


    this.showTabViews = function () {
        $jq("#menufavorites").removeClass("menulistareaon").addClass("menulistareaoff color-main");
        $jq("#menuyesvote").removeClass("menulistareaon").addClass("menulistareaoff color-main");
        $jq("#menuviews").removeClass("menulistareaoff color-main").addClass("menulistareaon");
        $jq("#YesVoteContent").hide();
        $jq("#ViewContent").show();
        $jq("#FavoriteContent").hide();
        PeopleMedia.Connect.CurrentTab = PeopleMedia.Connect.ViewsTab;
        //PeopleMedia.ReloadAds();
    };

    this.showTabYesVotes = function () {
        $jq("#menufavorites").removeClass("menulistareaon").addClass("menulistareaoff color-main");
        $jq("#menuyesvote").removeClass("menulistareaoff color-main").addClass("menulistareaon");
        $jq("#menuviews").removeClass("menulistareaon").addClass("menulistareaoff color-main");
        $jq("#YesVoteContent").show();
        $jq("#ViewContent").hide();
        $jq("#FavoriteContent").hide();
        PeopleMedia.Connect.CurrentTab = PeopleMedia.Connect.YesesTab;
        //PeopleMedia.ReloadAds();
    };

    this.showTabYesReceived = function () {
        $jq("#listYesReceived").removeClass("listpagelinksarea1off");
        $jq("#listYesReceived").addClass("listpagelinksarea1on");
        $jq("#listYesMutual").removeClass("listpagelinksarea2on");
        $jq("#listYesMutual").addClass("listpagelinksarea2off");
        $jq("#listYesSent").removeClass("listpagelinksarea3on");
        $jq("#listYesSent").addClass("listpagelinksarea3off");
    };
    this.showTabYesSent = function () {
        $jq("#listYesReceived").removeClass("listpagelinksarea1on");
        $jq("#listYesReceived").addClass("listpagelinksarea1off");
        $jq("#listYesMutual").removeClass("listpagelinksarea2on");
        $jq("#listYesMutual").addClass("listpagelinksarea2off");
        $jq("#listYesSent").removeClass("listpagelinksarea3off");
        $jq("#listYesSent").addClass("listpagelinksarea3on");
    };
    this.showTabYesMutual = function () {
        $jq("#listYesReceived").removeClass("listpagelinksarea1on");
        $jq("#listYesReceived").addClass("listpagelinksarea1off");
        $jq("#listYesMutual").removeClass("listpagelinksarea2off");
        $jq("#listYesMutual").addClass("listpagelinksarea2on");
        $jq("#listYesSent").removeClass("listpagelinksarea3on");
        $jq("#listYesSent").addClass("listpagelinksarea3off");
    };

    this.showTabFavoriteReceived = function () {
        $jq("#listFavoriteReceived").removeClass("listpagelinksarea1off");
        $jq("#listFavoriteReceived").addClass("listpagelinksarea1on");
        $jq("#listFavoriteMutual").removeClass("listpagelinksarea2on");
        $jq("#listFavoriteMutual").addClass("listpagelinksarea2off");
        $jq("#listFavoriteSent").removeClass("listpagelinksarea3on");
        $jq("#listFavoriteSent").addClass("listpagelinksarea3off");
    };
    this.showTabFavoriteSent = function () {
        $jq("#listFavoriteReceived").removeClass("listpagelinksarea1on");
        $jq("#listFavoriteReceived").addClass("listpagelinksarea1off");
        $jq("#listFavoriteMutual").removeClass("listpagelinksarea2on");
        $jq("#listFavoriteMutual").addClass("listpagelinksarea2off");
        $jq("#listFavoriteSent").removeClass("listpagelinksarea3off");
        $jq("#listFavoriteSent").addClass("listpagelinksarea3on");
    };
    this.showTabFavoriteMutual = function () {
        $jq("#listFavoriteReceived").removeClass("listpagelinksarea1on");
        $jq("#listFavoriteReceived").addClass("listpagelinksarea1off");
        $jq("#listFavoriteMutual").removeClass("listpagelinksarea2off");
        $jq("#listFavoriteMutual").addClass("listpagelinksarea2on");
        $jq("#listFavoriteSent").removeClass("listpagelinksarea3on");
        $jq("#listFavoriteSent").addClass("listpagelinksarea3off");
    };

    this.showTabViewsReceived = function () {
        $jq("#listViewsReceived").removeClass("listpagelinksarea1off");
        $jq("#listViewsReceived").addClass("listpagelinksarea1on");
        $jq("#listViewsSent").removeClass("listpagelinksarea3on");
        $jq("#listViewsSent").addClass("listpagelinksarea3off");
    };
    this.showTabViewsSent = function () {
        $jq("#listViewsReceived").removeClass("listpagelinksarea1on");
        $jq("#listViewsReceived").addClass("listpagelinksarea1off");
        $jq("#listViewsSent").removeClass("listpagelinksarea3off");
        $jq("#listViewsSent").addClass("listpagelinksarea3on");
    };

    this.initTextArea = function () {
        $jq(".connectWriteMessageTextArea").focus(function () {
            if ($jq(this).val() == "Write a quick message..." || $jq(this).val() == "Write a quick thank you for choosing you in Today's Matches..." || $jq(this).val() == "Write a quick thank you for viewing your profile..." || $jq(this).val() == "Write a quick thank you for saving your profile as a favorite...") {
                PeopleMedia.Connect.PlaceHolderText = $jq(this).val();
                $jq(this).val('');
            }
        });
        $jq(".connectWriteMessageTextArea").blur(function () {
            if ($jq(this).val() == '') {
                $jq(this).val(PeopleMedia.Connect.PlaceHolderText);
            }
        });
        $jq('.connectWriteMessageTextArea').keypress(function (e) {
            return PeopleMedia.Connect.PlainTextKeyPress(e, this);
        });
        $jq('.connectWriteMessageTextArea').bind('paste', function () {
            return PeopleMedia.Connect.PlainTextPaste(this);
        });
    };

    this.initFavorites = function (filter) {
        PeopleMedia.Connect.FavoriteLoaded = true;
        PeopleMedia.Connect.CurrentTab = PeopleMedia.Connect.FavoritesTab;
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentFavorite = (filter == PeopleMedia.Connect.SentFilter);
        PeopleMedia.Connect.ReceivedFavorite = (filter == PeopleMedia.Connect.ReceivedFilter);
        PeopleMedia.Connect.MutualFavorite = (filter == PeopleMedia.Connect.MutualFilter);
        if (!$jq("#favoriteincomingPopUp").is(":hidden")) {
            $jq("#favoritemutualPopUp").hide();
        }

    };

    this.initYesVotes = function (filter) {
        PeopleMedia.Connect.YesVotesLoaded = true;
        PeopleMedia.Connect.CurrentTab = PeopleMedia.Connect.YesesTab;
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentYes = (filter == PeopleMedia.Connect.SentFilter);
        PeopleMedia.Connect.ReceivedYes = (filter == PeopleMedia.Connect.ReceivedFilter);
        PeopleMedia.Connect.MutualYes = (filter == PeopleMedia.Connect.MutualFilter);
        if (!$jq("#yesincomingPopUp").is(":hidden")) {
            $jq("#yesmutualPopUp").hide();
        }
    };

    this.initViews = function (filter) {
        PeopleMedia.Connect.ViewLoaded = true;
        PeopleMedia.Connect.CurrentTab = PeopleMedia.Connect.ViewsTab;
        PeopleMedia.Connect.initTextArea();
        PeopleMedia.Connect.SentViews = (filter == PeopleMedia.Connect.SentFilter);
        PeopleMedia.Connect.ReceivedViews = (filter == PeopleMedia.Connect.ReceivedFilter);
        //alert(PeopleMedia.Connect.SentViews);alert(PeopleMedia.Connect.ReceivedViews);
    };

    this.init = function () {
        $jq("#menuyesvote").click(PeopleMedia.Connect.loadYesVotes);
        $jq("#menufavorites").click(PeopleMedia.Connect.loadFavorites);
        $jq("#menuviews").click(PeopleMedia.Connect.loadViews);
        PeopleMedia.Connect.initTextArea();
    };

    $jq(document).ready(function () {
        PeopleMedia.Connect.init();
    });
};



  
